package com.lsh.controller;

import com.lsh.domain.Register;
import com.lsh.domain.validation.group.MailGroup;
import com.lsh.domain.validation.group.PhoneGroup;
import com.lsh.service.RegisterService;
import com.lsh.util.result.Result;
import org.apache.commons.lang.StringUtils;
import org.jsoup.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 注册账号模块
 * @author Administrator
 * @date 2019/10/07 19:25
 */
@Controller
@RequestMapping("/registernew")
public class RegisterController {

    @Autowired
    RegisterService registerService;

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("/index")
    public String register(){
        return "register";
    }

    /**
     * 验证邮箱是否可用（是否已注册）
     * @param email
     * @return
     */
    @PostMapping("/mail")
    @ResponseBody
    public Result checkEmail(@RequestParam("email") String email) {
        boolean bool = registerService.checkEmail(email);
        if(!bool)
            return new Result(1001, "该邮箱可以使用");
        else
            return new Result(1002, "该邮箱已被注册");
    }

    /**
     * 邮箱注册方式
     * 校验验证码
     * @param code
     * @return
     */
    @GetMapping("/code")
    @ResponseBody
    public Result checkEmailCode(@RequestParam("code") String code, @RequestParam(name = "eToken", required = false) String token) {
        if(StringUtils.isEmpty(token))
            return new Result(1002, "验证码已过期");
        boolean bool = registerService.checkEmailCode(code, token);
        if(bool)
            return new Result(1001, "验证码正确");
        else
            return new Result(1002, "验证码错误");
    }

    /**
     * 验证用户名是否可用（是否已被注册）
     * @param username
     * @return
     */
    @PostMapping("/uname")
    @ResponseBody
    public Result checkUsername(@RequestParam("username") String username) {
        boolean bool = registerService.checkUsername(username);
        if(!bool)
            return new Result(1001, "该用户名可以使用");
        else
            return new Result(1002, "该用户名已被使用");
    }

    /**
     * 暂存注册信息
     * @param register
     * @return
     */
    @PostMapping("/create/em")
    @ResponseBody
    public Result stagUser(@Validated(MailGroup.class) Register register, BindingResult bindingResult) {
        ArrayList<String> errors = new ArrayList<>();
        if(bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for(ObjectError error: allErrors){
                errors.add(error.getDefaultMessage());
            }
            return new Result(1002, "注册失败", new HashMap<>()).addExtraData("errors", errors);
        }
        registerService.saveUserAndSendEmail(register);
        return new Result(1001, "注册成功");
    }

    /**
     * 检查手机号码是否可用
     * @param phone
     * @return
     */
    @PostMapping("/phone")
    @ResponseBody
    public Result checkPhone(@RequestParam("phone") String phone) {
        boolean bool = registerService.checkPhone(phone);
        if(bool)
            return new Result(1001, "该手机号码可用");
        else
            return new Result(1002, "该手机号码已被注册");
    }

    /**
     * 请求服务器发送注册验证码
     * @param phone
     * @return
     */
    @PostMapping("/smsCode")
    @ResponseBody
    public Result sendSmsCode(@RequestParam("phone") String phone){
        int codeTime = 120;
        boolean bool = registerService.sendRegisterCode(phone, codeTime);
        if(bool)
            return new Result(1001, "请留意手机短信", new HashMap<>()).addExtraData("time", codeTime);
        else
            return new Result(1002, "发送失败");
    }

    /**
     * 校验注册的手机号码和验证码是否一致
     */
    @GetMapping("/phoneandcode")
    @ResponseBody
    public Result checkPhoneAndCode(@RequestParam("phone") String phone, @RequestParam("code") String code) {
        if(StringUtils.isEmpty(phone))
            return new Result(1003, "请输入手机号码");
        else if(StringUtils.isEmpty(code))
            return new Result(1002, "请输入短信验证码");
        else {
            boolean bool = registerService.checkPhoneAndCode(phone, code);
            if(bool)
                return new Result(1001, "校验成功");
            else
            return new Result(1002, "验证码错误");
        }
    }

    /**
     * 创建用户（手机号码注册）
     * @param register
     * @return
     */
    @PostMapping("/create/ph")
    @ResponseBody
    public Result createUser(@Validated(PhoneGroup.class) Register register, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            ArrayList<String> errors = new ArrayList<>();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for(ObjectError error: allErrors){
                errors.add(error.getDefaultMessage());
            }
            return new Result(1003, "注册失败", new HashMap<>()).addExtraData("errors", errors);
        }
        boolean bool = registerService.createUser(register);
        if(bool)
            return new Result(1001, "注册成功");
        else
            return new Result(1002, "注册失败");
    }

}
